import {
	defineStore
} from 'pinia';

export const useUserStore = defineStore('user', {
	// 状态：定义 Store 中的数据
	state: () => {
		return {
			name: null,
			token: null,
		}
	},

	// 获取器：类似于计算属性，用于获取状态的派生值
	getters: {
		getName: (state) => state.name,
	},

	// 动作：用于修改状态的方法
	actions: {
		setName(name) {
			this.name = name
			localStorage.setItem('name', name)
		},

		setToken(token) {
			this.token = token
			localStorage.setItem('token', token)
		},

		removeName() {
			this.name = null
			localStorage.removeItem('name')
		},

		removeToken() {
			this.token = null
			localStorage.removeItem('token')
		},

		logout() {
			this.removeName()
			this.removeToken()
		}
	},

	// 持久化配置	
	persist: true,

});